package com.okcl.security;

import com.okcl.entity.*;
import com.okcl.mapper.*;
import jakarta.annotation.Resource;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 从数据库中获取用户信息
 * 将信息送到security上下文
 */
@Service
@Transactional
public class UserDetailsServiceImpl implements UserDetailsService {
    @Resource
    private UserMapper userMapper;
    @Resource
    private RoleMapper roleMapper;
    @Resource
    private UserRoleMapper userRoleMapper;
    @Resource
    private RolePermissionMapper rolePermissionMapper;
    @Resource
    private PermissionMapper permissionMapper;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        User user = userMapper.getUserByUsername(username);
        if (Objects.isNull(user)) {
            throw new UsernameNotFoundException("用户不存在");
        }
        //当前用户角色名称列表
        List<String> roleNameList = new ArrayList<>();
        //当前用户的权限名称列表
        List<String> permissionNameList = new ArrayList<>();
        // 1.获取用户角色列表集合
        List<UserRole> userRoles = userRoleMapper.getUserRoleListByUserId(user.getUserId());
        if (!CollectionUtils.isEmpty(userRoles)) {
            //取出角色id
            List<Long> roleIds = userRoles.stream().map(UserRole::getRoleId).toList();
            if (!CollectionUtils.isEmpty(roleIds)) {
                List<Role> roles = roleMapper.batchGetRolesByRoleIds(roleIds);
                if (!CollectionUtils.isEmpty(roles)) {
                    List<String> roleNames = roles.stream().map(Role::getRoleName).toList();
                    //将查询到的角色名称保存到到roleNameList中
                    roleNameList.addAll(roleNames);
                }
                //2.获取用户权限列表
                List<RolePermission> rolePermissions = rolePermissionMapper.batchGetRolePermissionsByRoleIds(roleIds);
                if (!CollectionUtils.isEmpty(rolePermissions)) {
                    //获取权限表下的所有权限
                    List<Long> permissionIds = rolePermissions.stream().map(RolePermission::getPermissionId).toList();
                    if (!CollectionUtils.isEmpty(permissionIds)) {
                        List<Permission> permissions = permissionMapper.batchGetPermissionsByPermissionIds(permissionIds);
                        if (!CollectionUtils.isEmpty(permissions)) {
                            List<String> permissionNames = permissions.stream().map(Permission::getPermissionName).toList();
                            //将查询到的权限名称保存到permissionNameList中
                            permissionNameList.addAll(permissionNames);
                        }
                    }
                }
            }
        }


        return new LoginUserDetails(user, roleNameList, permissionNameList);
    }
}
